package com.core.zyyz.pcmc.service.impl;

import java.util.*;
import java.util.stream.Collectors;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.map.MapUtil;
import com.core.common.utils.StringUtils;
import com.core.zyyz.enums.BooleanEnum;
import com.core.zyyz.enums.NodeType;
import com.core.zyyz.pcmc.domain.DataPermissionEntity;
import com.core.zyyz.pcmc.service.PcmcDeptService;
import com.core.zyyz.pcmc.service.PcmcOrgHierarchyService;
import com.core.zyyz.pcmc.service.PcmcUserService;
import com.google.common.collect.Maps;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.core.zyyz.pcmc.mapper.PcmcOrgHierarchyMapper;
import com.core.zyyz.pcmc.domain.PcmcOrgHierarchy;

/**
 * 机构层级Service业务层处理
 *
 * @author LiuFushun
 * @date 2024-10-29
 */
@Service
public class PcmcOrgHierarchyServiceImpl implements PcmcOrgHierarchyService
{
    @Autowired
    private PcmcOrgHierarchyMapper pcmcOrgHierarchyMapper;
    @Autowired
    private PcmcDeptService pcmcDeptService;
    @Autowired
    private PcmcUserService pcmcUserService;

    /**
     * 查询机构层级
     *
     * @param ID 机构层级主键
     * @return 机构层级
     */
    @Override
    public PcmcOrgHierarchy selectPcmcOrgHierarchyByID(String ID)
    {
        return pcmcOrgHierarchyMapper.selectPcmcOrgHierarchyByID(ID);
    }

    /**
     * 查询机构层级列表
     *
     * @param pcmcOrgHierarchy 机构层级
     * @return 机构层级
     */
    @Override
    public List<PcmcOrgHierarchy> selectPcmcOrgHierarchyList(PcmcOrgHierarchy pcmcOrgHierarchy)
    {
        return pcmcOrgHierarchyMapper.selectPcmcOrgHierarchyList(pcmcOrgHierarchy);
    }

    /**
     * 新增机构层级
     *
     * @param pcmcOrgHierarchy 机构层级
     * @return 结果
     */
    @Override
    public int insertPcmcOrgHierarchy(PcmcOrgHierarchy pcmcOrgHierarchy)
    {
        return pcmcOrgHierarchyMapper.insertPcmcOrgHierarchy(pcmcOrgHierarchy);
    }

    /**
     * 修改机构层级
     *
     * @param pcmcOrgHierarchy 机构层级
     * @return 结果
     */
    @Override
    public int updatePcmcOrgHierarchy(PcmcOrgHierarchy pcmcOrgHierarchy)
    {
        return pcmcOrgHierarchyMapper.updatePcmcOrgHierarchy(pcmcOrgHierarchy);
    }

    /**
     * 批量删除机构层级
     *
     * @param IDs 需要删除的机构层级主键
     * @return 结果
     */
    @Override
    public int deletePcmcOrgHierarchyByIDs(String[] IDs)
    {
        return pcmcOrgHierarchyMapper.deletePcmcOrgHierarchyByIDs(IDs);
    }

    /**
     * 删除机构层级信息
     *
     * @param ID 机构层级主键
     * @return 结果
     */
    @Override
    public int deletePcmcOrgHierarchyByID(String ID)
    {
        return pcmcOrgHierarchyMapper.deletePcmcOrgHierarchyByID(ID);
    }

    /**
     * 查询组织机构或机构下的人员名单
     * @param permission
     * @param paraMap
     * @return
     */
    public List<PcmcOrgHierarchy> queryOrgHierarchyTreeNode(DataPermissionEntity permission,PcmcOrgHierarchy paraMap) {
         List<PcmcOrgHierarchy> hierarchyList = null;
            if (StringUtils.equals(paraMap.getParentNodeCode(), "*") && NodeType.DEPARTMENT.is(paraMap.getNodeType())) {
                 hierarchyList = queryOrgHierarchy(paraMap, BooleanEnum.FALSE.getFlag(), permission);
                if (CollUtil.isNotEmpty(hierarchyList)) {
                      Set<String> nodeCodeSet = (Set<String>)hierarchyList.stream().map(PcmcOrgHierarchy::getNodeCode).collect(Collectors.toSet());
                      hierarchyList = (List<PcmcOrgHierarchy>)hierarchyList.stream().filter(h -> !CollUtil.contains(nodeCodeSet, h.getParentNodeCode())).collect(Collectors.toList());
                }
            } else {
                hierarchyList = queryOrgHierarchy(paraMap, BooleanEnum.TRUE.getFlag(), permission);
            }
            if (CollUtil.isEmpty(hierarchyList)) {
                  return Collections.emptyList();
            }

            List<String> nodeCodeList = (List<String>)hierarchyList.stream().map(PcmcOrgHierarchy::getNodeCode).collect(Collectors.toList());
            Map<String, String> nameMap = Maps.newHashMap();
            if (NodeType.DEPARTMENT.is(paraMap.getNodeType())) {
                nameMap = pcmcDeptService.queryDeptNameMap(nodeCodeList);
            }
            if (NodeType.EMPLOYEE.is(paraMap.getNodeType())) {
                nameMap = pcmcUserService.queryEmployeeNameMap(nodeCodeList);
            }

            for (PcmcOrgHierarchy hierarchy : hierarchyList) {
              hierarchy.setIsLeaf(Boolean.valueOf((hierarchy.getDeptCount().intValue() == 0)));
              hierarchy.setNodeName(MapUtil.getStr(nameMap, hierarchy.getNodeCode(), hierarchy.getNodeCode()));
              hierarchy.setNodeNameShow(StringUtils.join((Object[])new String[] { "[", hierarchy.getNodeCode(), "]", MapUtil.getStr(nameMap, hierarchy.getNodeCode(), hierarchy.getNodeCode()) }));
            }
         return hierarchyList;
    }

    private List<PcmcOrgHierarchy> queryOrgHierarchy(PcmcOrgHierarchy paraMap, String onlyOneLevel, DataPermissionEntity permissionEntity) {
        List<PcmcOrgHierarchy> list = null;
        if (BooleanEnum.TRUE.is(onlyOneLevel)) {
            list = pcmcOrgHierarchyMapper.selectPcmcOrgHierarchyList(paraMap);
        }else if (StringUtils.equals(paraMap.getParentNodeCode(), "*")){
            list = pcmcOrgHierarchyMapper.selectPcmcOrgHierarchyList(paraMap);
        }else {
            paraMap.setNodeType(NodeType.DEPARTMENT.getValue());
            PcmcOrgHierarchy orgHierarchy = pcmcOrgHierarchyMapper.selectPcmcOrgHierarchy(paraMap);
            if (Objects.isNull(orgHierarchy)) {
                return Collections.emptyList();
            }
            paraMap.setNodeSeq(orgHierarchy.getNodeSeq());
            pcmcOrgHierarchyMapper.selectPcmcOrgHierarchyList(paraMap);
        }
        return list;
    }

}
